import { rulesOptions, rulesOptionsMap, selectOptions, typeOptions } from '../options';
import { CreatedOptionsProps } from '@/views/basic/created/interface';
import { initDefaultOptions, backfillMethod } from '../backfillMethod';

/**验证规则修改 */
const changeRulesOp = (basicForm, form) => {
  const flag = form['_rules'] !== 'customRequired';
  formChangeItemStatus(basicForm, ['_rulesCustom'], flag, 'hide');
};

/**公共属性 */
const publicItem: CreatedOptionsProps[] = [
  {
    type: 'select',
    label: '类型',
    prop: 'type',
    hide: false,
    sort: 20000,
    children: typeOptions,
    changeMethod: (form, itemForm) => {
      nextTick(() => {
        itemForm.options = initDefaultOptions(itemForm.selectItem, true);
        backfillMethod(itemForm);
      });
    },
    rules: [
      {
        required: true,
        message: '类型',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '标签',
    prop: 'label',
    sort: 19900,
    hide: false,
    rules: [
      {
        required: true,
        message: '标签',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'select',
    label: '是否隐藏',
    prop: 'hide',
    hide: false,
    sort: 19800,
    defaultValue: false,
    children: selectOptions,
    rules: [
      {
        required: true,
        message: '是否隐藏',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '绑定字段',
    prop: 'prop',
    sort: 19700,
    hide: false,
    rules: [
      {
        required: true,
        message: '绑定字段',
        trigger: 'blur'
      }
    ]
  },
  {
    type: 'input',
    label: '默认值',
    prop: 'defaultValue',
    sort: 19600,
    hide: false
  },
  {
    type: 'select',
    label: '默认值类型',
    prop: 'defaultValueType',
    sort: 19500,
    defaultValue: 'text',
    hide: false,
    children: [
      {
        label: '文本',
        value: 'text',
        type: 'option'
      },
      {
        label: '数字',
        value: 'number',
        type: 'option'
      }
    ]
  },
  {
    type: 'input',
    label: '占位符',
    sort: 19400,
    prop: 'attrs.placeholder',
    hide: false
  },
  {
    type: 'select',
    label: '是否禁用',
    prop: 'attrs.disabled',
    hide: false,
    sort: 19300,
    defaultValue: false,
    children: selectOptions
  },
  {
    type: 'select',
    sort: 19200,
    label: '是否可清空',
    prop: 'attrs.clearable',
    hide: false,
    defaultValue: true,
    children: selectOptions
  },
  {
    type: 'select',
    label: '是否必填',
    sort: 19100,
    prop: '_rules',
    hide: false,
    attrs: {
      clearable: true
    },
    changeMethod: (form, itemForm) => {
      const value = form['_rules'];
      changeRulesOp(itemForm, form);
      if (value) {
        const rValue = form._rules !== 'customRequired' ? [rulesOptionsMap[value]] : '';
        form.rules = rValue;
      } else {
        form.rules = '';
      }
    },
    children: rulesOptions
  },
  {
    type: 'input',
    sort: 19000,
    label: '自定义规则',
    prop: '_rulesCustom',
    hide: true,
    defaultValue: '',
    changeMethod: form => {
      const value = form['_rulesCustom'];
      if (value) {
        const rValue = value ? JSON.parse(value) : '';
        form.rules = rValue;
      } else {
        form.rules = '';
      }
    },
    attrs: {
      type: 'textarea'
    }
  },
  {
    type: 'input',
    sort: 18900,
    label: '宽度',
    prop: 'attrs.style.width',
    hide: false,
    defaultValue: ''
  }
];

export { publicItem };
